package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;

    /**
     *  接口名称：用户通用设置 - 读取
     */
    public ResponseEntity<Object> querySettings(){
        //获取当前认证后的用户
        User user = UserHolder.get();

        //根据用户id查询同志设置表 tb_settings
        Settings settings = settingsApi.findByUserId(user.getId());

        //根据用户id查询陌生人问题
        Question question = questionApi.findByUserId(user.getId());

        //创建vo，封装返回结果
        SettingsVo settingsVo = new SettingsVo();
        //来自于：db
        BeanUtils.copyProperties(settings,settingsVo);

        settingsVo.setPhone(user.getMobile());
        //设置陌生人问题
        if (question == null) {
            settingsVo.setStrangerQuestion("你喜欢我吗？");
        }else {
            settingsVo.setStrangerQuestion(question.getTxt());
        }

        return ResponseEntity.ok(settingsVo);
    }

    public ResponseEntity<Object> saveNotifications(Settings param) {
        //获取认证后的用户id
        Long userId = UserHolder.getUserId();

        //根据id查询用户设置表
        Settings settings = settingsApi.findByUserId(userId);

        if (settings == null){
            //没有查询到数据，执行添加
            settings = new Settings();
            //对象拷贝
            BeanUtils.copyProperties(param,settings);
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else {
            //该用户设置已存在，执行修改
            settings.setLikeNotification(param.getLikeNotification());
            settings.setGonggaoNotification(param.getGonggaoNotification());
            settings.setPinglunNotification(param.getPinglunNotification());
            //执行修改
            settingsApi.update(settings);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 设置陌生人问题
     * @param content
     * @return
     */
    public ResponseEntity<Object> saveQuestions(String content) {
        //获取认证用户
        Long userId = UserHolder.getUserId();

        //根据用户id查询陌生人问题
        Question question = questionApi.findByUserId(userId);

        //判断
        if (question == null){
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            //执行保存
            questionApi.save(question);
        }else {
            question.setTxt(content);
            //执行修改
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单分页
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity<Object> blacklist(Integer page, Integer pagesize) {
        //获取认证用户
        User user = UserHolder.get();

        //根据用户id查询黑名单服务
        IPage<UserInfo> iPage = blackListApi.blacklist(page,pagesize,user.getId());

        //封装返回结果
        PageResult pageResult = new PageResult(
                page,pagesize,(int)iPage.getTotal(), iPage.getRecords()
        );
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：黑名单 - 移除
     * @param blackUserId
     * @return
     */
    public ResponseEntity<Object> deleteBlackUser(Long blackUserId) {
        //获取当前用户
        Long userId = UserHolder.getUserId();

        //调用dubbo服务，从黑名单列表中移除黑名单用户
        blackListApi.deleteBlackUser(userId,blackUserId);
        return null;
    }
}
